/**
 * Created by joustany on 17/9/30.
 */
var params = JSON.parse(window.localStorage.users)
var id = params.id;
var name = params.name
var access_token = params.access_token
document.title = name
//get数据
$.ajax({
    type: "POST",
    url: api('/stock-bill/get-check-list'),
    data: {
        access_token:access_token,
        id:id,
    },
    dataType: "json",
    success: function (res) {
        console.log(res)
        stockList(res.data.list)
    }
});
//渲染页面
function stockList(data) {
    var html = ''
    $.each(data, function (index, item) {
        // console.log(item)
        if(item.stock<0){
            item.stock = 0
        }
        html += '<li class="food-item" data-id="'+ item.item_id +'">'
        html += '<div class="food-image">'
        html += '<img src="'+item.img+'" alt="">'
        html += '</div>'
        html += '<div class="food-typeFace">'
        html += '<div>'+item.name+'</div>'
        html += '<div>库存:<span class="show_num">'+item.stock+'</span></div>'
        if(item.stock>0){
            html += '<input class="placeholder" type="tel" placeholder="请输入当前数量" onfocus="enterHandle(this)" onblur="comeHandle(this)">'
        }else {
            html += '<input class="placeholder box" type="tel" value="当前:'+ item.stock +'" onfocus="enterHandle(this)" onblur="comeHandle(this)">'
        }
        html += '</div>'
        html += '</li>'
        $('.food-list').html(html)
    })
}
var input_num
function enterHandle(item) {
    input_num = item.value
    if(item.value){
        item.value= ''
        item.placeholder ='请输入库存数量'
    }
}
function comeHandle(item){
    // console.log(input_num.parseInt())
    var num = parseInt(input_num.replace(/[^0-9]/ig,""))
    if(item.value){
        item.value='当前:'+item.value+''
        item.style.border = '1px solid #f5a623'
    }else if(!num){
        item.placeholder ='请输入库存数量'
    } else {
        item.value='当前:' + num +''
    }
}
//点击提交数据
function seveHandle(){
    var data = []
    var flag = true;
    $('input').each(function (i,item) {
        if(!item.value){
            layer.open({
                content: '请输入盘点的数量'
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
            flag = false;
            return;
        }
    })
    if(!flag){
        return;
    }
        $('input').each(function (i,item) {
            console.log($(this))
            var num
            if(!item.value){
                num = 0
            }else {
                num = item.value.split(':')[1]
            }
            // console.log(num)
            var food = {}
            food.id = $(this).parents('.food-item').attr('data-id')
            food.show_num = $(this).parents('.food-item').find('.show_num').text()
            food.name = $(this).parents('.food-item')[0].innerText.split('\n')[1]
            food.num = num
            console.log(food)
            data.push(food)
        })
        var jsonData = JSON.stringify(data)
        layer.open({
            content: '请核对所有产品库存是否正确'
            ,btn: ['已核对准确', '重新核对']
            ,yes: function(index){
                // console.log(index)
                foodPost(jsonData)
                layer.close(index);
            }
        });

}
//post数据
function foodPost(data) {
    console.log(data)
    $.ajax({
        type: "POST",
        url: api('/stock-bill/create'),
        data: {
            access_token:access_token,
            id:id,
            type:'4',
            items:data,
        },
        dataType: "json",
        success: function (res) {
            console.log(res)
            if(res.success){
                window.location.href = './paveMessage.html?v'+ random +''
            }else {
                layer.open({
                    content: res.message
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }
        }
    });
}
